﻿using System;
using System.Web.UI;
using System.Reflection;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Collections.Generic;

using ExtraClub.Domain;
using ExtraClub.Domain.Entities;

using SubSonic;
using ExtraClub.Domain.Forms;
using ExtraClub.Domain.Framework;

namespace ExtraClub.Web.Forms.client {
    public partial class module : BaseForm {
        protected VwClientProfile SelectedEntity { get; set; }
        protected int SelectedClubId { get; set; }
        protected override void OnLoad(EventArgs e) {

            if (Request.QueryString.Count > 0 && Request.QueryString["id"] != null) {
                SelectedEntity = VwClientProfile.FetchByID(int.Parse(Request.QueryString["id"]));
            }
            if (Request.QueryString.Count > 0 && Request.QueryString["clubid"] != null) {
                SelectedClubId = int.Parse(Request.QueryString["clubid"]);
            }
            if (SelectedClubId == 0) {
                SelectedClubId = SiteContext.Current.Club.Id;
            }
            if (CurrentCommand == ECommand.New) {
                SelectedEntity = null;
            }

            base.OnLoad(e);
        }
        protected override string SetFields() {
            string ret = "null";
            switch (CurrentCommand) {
                case ECommand.New:
                    ret = BuildSaveForm(false);
                    break;
                case ECommand.Edit:
                    if (SelectedEntity != null) {
                        ret = BuildSaveForm(true);
                    }
                    break;
                case ECommand.Delete:
                    if (SelectedEntity != null) {
                        ret = BuildDeletForm();
                    }
                    break;
            }
            return ret;
        }

        private string BuildDeletForm() {
            FieldCollection fs = new FieldCollection();
            AddHiddenFieldes(ref fs, SelectedEntity.Id.ToString());

            return fs.JSON();
        }
        private string BuildSaveForm(bool edit) {

            FieldCollection fs = new FieldCollection();
            fs.Control = this;
            fs.LabelKeySuffex = "-label";

            AddHiddenFieldes(ref fs, SelectedEntity != null ? SelectedEntity.Id.ToString() : "");

            /* Club Id */
            new Field(FieldType.Hidden, "clubid", f =>
            {
                f.Value = SelectedClubId.ToString();
            }, fs);

            /* First Name */
            new Field(FieldType.String, Client.Columns.FirstName.ToLower(), f =>
            {
                f.Size = Client.Schema.GetColumn(Client.Columns.FirstName).MaxLength;
                f.Required = true;
                f.Value = edit ? SelectedEntity.FirstName : String.Empty;
            }, fs);

            /* Family Name */
            new Field(FieldType.String, Client.Columns.FamilyName.ToLower(), f =>
            {
                f.Size = Client.Schema.GetColumn(Client.Columns.FamilyName).MaxLength;
                f.Required = true;
                f.Value = edit ? SelectedEntity.FamilyName : String.Empty;
            }, fs);

            /* ID Number */
            new Field(FieldType.Integer, Client.Columns.IDNumber.ToLower(), f =>
            {
                f.Size = 10;
                f.MinLength = 5;
                f.Required = true;
                f.Value = edit ? SelectedEntity.IDNumber.ToString() : String.Empty;
            }, fs);

            /* Sex */
            new Field(FieldType.Select, Client.Columns.Sex.ToLower(), f =>
            {
                f.SelectOptions = UISelectOptions[f.LabelKeySuffex].ToArray();
                f.SelectValues = UISelectValues[f.LabelKeySuffex].ToArray();
                f.Required = true;
                f.Value = edit ? SelectedEntity.Sex.ToString() : String.Empty;
            }, fs);

            /* BirthDate */
            new Field(FieldType.DateSplit,Client.Columns.BirthDate.ToLower(), f =>
            {
                f.DateFormat = "d/m/Y";
                f.Required = true;
                f.Value = edit ? ((DateTime)SelectedEntity.BirthDate).ToString("MM/dd/yyyy") : String.Empty;
            }, fs);

            /* MarrageState */
            new Field(FieldType.Select,Client.Columns.MarrageState.ToLower(), f =>
            {
                f.SelectOptions = UISelectOptions[f.LabelKeySuffex].ToArray();
                f.SelectValues = UISelectValues[f.LabelKeySuffex].ToArray();
                f.Required = true;
                f.Value = edit ? SelectedEntity.MarrageState.ToString() : String.Empty;
            }, fs);

            /* Number of Children */
            new Field(FieldType.Integer,Client.Columns.NumberOfChildren.ToLower(), f =>
            {
                f.Size = 2;
                f.Required = false;
                f.Value = edit ? SelectedEntity.NumberOfChildren.ToString() : String.Empty;
            }, fs);

            /* Education */
            new Field(FieldType.Select,Client.Columns.Education.ToLower(), f =>
            {
                f.SelectOptions = UISelectOptions[f.LabelKeySuffex].ToArray();
                f.SelectValues = UISelectValues[f.LabelKeySuffex].ToArray();
                f.Required = true;
                f.Value = edit ? SelectedEntity.Education.ToString() : String.Empty;
            }, fs);

            /* Mother's Family Name */
            new Field(FieldType.String,Client.Columns.MothersFamilyName.ToLower(), f =>
            {
                f.Size = Client.Schema.GetColumn(Client.Columns.MothersFamilyName).MaxLength;
                f.Required = false;
                f.Value = edit ? SelectedEntity.MothersFamilyName : String.Empty;
            }, fs);

            /* City/Region */
            new Field(FieldType.Select, AddressRepository.Columns.CityRegion.ToLower(), f =>
            {
                f.SelectOptions = UISelectOptions[f.LabelKeySuffex].ToArray();
                f.SelectValues = UISelectValues[f.LabelKeySuffex].ToArray();
                f.Required = true;
                f.Value = edit ? SelectedEntity.CityRegion.ToString() : String.Empty;
            }, fs);

            /* House Number */
            new Field(FieldType.String, AddressRepository.Columns.HouseNumber.ToLower(), f =>
            {
                f.Size = 4;
                f.Required = true;
                f.Value = edit ? SelectedEntity.HouseNumber : String.Empty;
            }, fs);

            /* Apartment */
            new Field(FieldType.Integer, AddressRepository.Columns.Apartment.ToLower(), f =>
            {
                f.Size = 4;
                f.Required = false;
                f.Value = edit ? SelectedEntity.Apartment.ToString() : String.Empty;
            }, fs);

            /* ZIP */
            new Field(FieldType.Integer, AddressRepository.Columns.Zip.ToLower(), f =>
            {
                f.Size = 5;
                f.MinLength = 5;
                f.Required = true;
                f.Value = edit ? SelectedEntity.Zip.ToString() : String.Empty;
            }, fs);

            #region Phones
            /* Cellular Phone */
            new Field(FieldType.Combined, AddressRepository.Columns.CellularPhone.ToLower(), f =>
            {
                f.Required = true;
                f.Fields = new Field[2];

                List<string> options = UISelectOptions[f.LabelKeySuffex];
                /* Prefix */
                new Field(FieldType.Select, null, a =>
                {
                    a.SelectOptions = options.ToArray();
                    a.SelectValues = UISelectValues[f.LabelKeySuffex].ToArray();
                    a.Value = edit ? ((int)SelectedEntity.CellularPhonePrefix).ToString("0##") : String.Empty;
                    f.Fields[0] = a;
                }, null);

                /* Phone */
                new Field(FieldType.Number, null, b =>
                {
                    b.Size = 10;
                    b.Value = edit ? SelectedEntity.CellularPhone.ToString() : String.Empty;
                    f.Fields[1] = b;
                }, null);

            }, fs);

            /* Home Phone */
            new Field(FieldType.Combined, AddressRepository.Columns.HomePhone.ToLower(), f =>
            {
                f.Required = true;
                f.Fields = new Field[2];

                List<string> options = UISelectOptions[f.LabelKeySuffex];
                /* Prefix */
                new Field(FieldType.Select, null, a =>
                {
                    a.SelectOptions = options.ToArray();
                    a.SelectValues = UISelectValues[f.LabelKeySuffex].ToArray();
                    a.Value = edit ? ((int)SelectedEntity.HomePhonePrefix).ToString("0#") : String.Empty;
                    f.Fields[0] = a;
                }, null);

                /* Phone */
                new Field(FieldType.Number, null, b =>
                {
                    b.Size = 10;
                    b.Value = edit ? SelectedEntity.HomePhone.ToString() : String.Empty;
                    f.Fields[1] = b;
                }, null);
            }, fs);
            #endregion

            /* Email */
            new Field(FieldType.Email, AddressRepository.Columns.Email.ToLower(), f =>
            {
                f.Size = AddressRepository.Schema.GetColumn(AddressRepository.Columns.Email).MaxLength;
                f.Required = true;
                f.Value = edit ? SelectedEntity.Email : String.Empty;
            }, fs);

            return fs.JSON();
        }
    }
}
